package com.owen.system.keep.account.controller;

import com.owen.common.utils.PageUtils;
import com.owen.common.utils.Query;
import com.owen.common.utils.R;
import com.owen.system.keep.account.dto.KeepAccountDTO;
import com.owen.system.keep.account.service.KeepAccountService;
import com.owen.system.keep.account.vo.KeepAccountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "我的账户")
@RequestMapping("/keep/account")
public class KeepAccountController {

    @Autowired
    private KeepAccountService keepAccountService;

    @GetMapping
    @RequiresPermissions("keep:account:list")
    @ApiOperation(value = "进入我的账户列表页面", notes = "进入我的账户列表页面", httpMethod = "GET")
    String listPage() {
        return "keep/account/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("keep:account:list")
    @ApiOperation(value = "我的账户列表数据", notes = "我的账户列表数据", httpMethod = "GET")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<KeepAccountVO> keepAccountList = keepAccountService.list(query);
        int total = keepAccountService.count(query);
        PageUtils pageUtils = new PageUtils(keepAccountList, total);
        return pageUtils;
    }

    @ResponseBody
    @RequiresPermissions("keep:account:list")
    @ApiOperation(value = "我的账户列表", notes = "我的账户列表", httpMethod = "GET")
    @GetMapping("/myAccount")
    public List<KeepAccountVO> myAccount() {
        return keepAccountService.findMyAccount();
    }

    @GetMapping("/add")
    @RequiresPermissions("keep:account:add")
    @ApiOperation(value = "进入到我的账户新增页面", notes = "进入到我的账户新增页面", httpMethod = "GET")
    String add() {
        return "keep/account/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("keep:account:edit")
    @ApiOperation(value = "进入我的账户编辑页面", notes = "进入我的账户编辑页面", httpMethod = "GET")
    String edit(@PathVariable("id") String id, Model model) {
        KeepAccountVO keepAccount = keepAccountService.get(id);
        model.addAttribute("keepAccount", keepAccount);
        return "keep/account/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("keep:account:add")
    @ApiOperation(value = "我在账户新增", notes = "我的账户新增", httpMethod = "POST")
    public R save(KeepAccountDTO dto) {
        if (keepAccountService.save(dto) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/update")
    @ApiOperation(value = "修改个人账户", notes = "修改个人账户", httpMethod = "POST")
    @RequiresPermissions("keep:account:edit")
    public R update(KeepAccountDTO dto) {
        keepAccountService.update(dto);
        return R.ok();
    }

    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("keep:account:remove")
    @ApiOperation(value = "删除个人账户", notes = "删除个人账户", httpMethod = "POST")
    public R remove(String id) {
        if (keepAccountService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("keep:account:batchRemove")
    @ApiOperation(value = "批量删除个人账户", notes = "批量删除个人账户", httpMethod = "POST")
    public R remove(@RequestParam("ids[]") String[] ids) {
        keepAccountService.batchRemove(ids);
        return R.ok();
    }

}
